home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / screen-resolution-extra / policyui.py < prev    next >
Encoding:
Python Source  |  2009-04-06  |  8.9 KB  |  281 lines

  1. # -*- coding: utf-8 -*-
  2. ## Copyright (C) 2001-2008 Alberto Milone <albertomilone@alice.it>
  3.  
  4. ## This program is free software; you can redistribute it and/or modify
  5. ## it under the terms of the GNU General Public License as published by
  6. ## the Free Software Foundation; either version 2 of the License, or
  7. ## (at your option) any later version.
  8.  
  9. ## This program is distributed in the hope that it will be useful,
  10. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. ## GNU General Public License for more details.
  13.  
  14. ## You should have received a copy of the GNU General Public License
  15. ## along with this program; if not, write to the Free Software
  16. ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18.  
  19. import gtk, gobject, sys, dbus, logging
  20. import ScreenResolution
  21. from ScreenResolution.policykit import PolicyKitAuthentication
  22. from ScreenResolution import ui
  23.  
  24. POLICY_KIT_ACTION = 'com.ubuntu.screenresolution.mechanism.configure'
  25. SERVICE_NAME   = 'com.ubuntu.ScreenResolution.Mechanism'
  26. OBJECT_PATH    = '/'
  27. INTERFACE_NAME = 'com.ubuntu.ScreenResolution.Mechanism'
  28. usage = 'python policyui.py 1024x768'
  29.  
  30. import os
  31. import sys
  32. from subprocess import Popen, PIPE
  33.  
  34. import XKit
  35. from XKit import xutils, xorgparser
  36.  
  37. clean = False
  38.  
  39. def checkVirtual(virtres):
  40.     source = '/etc/X11/xorg.conf'
  41.     
  42.     try:
  43.         a = xutils.XUtils(source)
  44.     except(IOError, XKit.xorgparser.ParseException):#if xorg.conf is missing or broken
  45.         return False
  46.     
  47.     if len(a.globaldict['Screen']) > 0:
  48.         '''
  49.         See if the virtual resolution is already there and if it's big enough
  50.         '''
  51.         res = None
  52.         for screen in a.globaldict['Screen']:
  53.             try:
  54.                 res = a.getValue('SubSection', 'virtual', 0, identifier='Display', sect="Screen", reference=None)
  55.                 if res:
  56.                     if 'x' in res.lower():
  57.                         res = res.lower().split('x')
  58.                     elif ' ' in res.lower().strip():
  59.                         res = res.lower().split(' ')
  60.                         res = filter(lambda x: x != '', res)
  61.                         if len(res) == 2 and int(virtres[0]) <= int(res[0]) and int(virtres[1]) <= int(res[1]):
  62.                             '''
  63.                             Nothing to do, the virtual resolution is already there
  64.                             '''
  65.                             return True
  66.             except (XKit.xorgparser.SectionException, XKit.xorgparser.OptionException, AttributeError):
  67.                 pass
  68.     
  69.     return False
  70.  
  71. def computeVirtual(*params):
  72.     '''
  73.     Compute the virtual resolution on the basis of the
  74.     position and resolution of the screens.
  75.     '''
  76.     xResolutions = []
  77.     yResolutions = []
  78.     for param in params[0]:
  79.         position = param[: str(param).find(':')].split(',')
  80.         relativeResolution = param[param.find(':') +1:].split('x')
  81.         xres = int(position[0]) + int(relativeResolution[0])
  82.         yres = int(position[1]) + int(relativeResolution[1])
  83.         xResolutions.append(xres)
  84.         yResolutions.append(yres)
  85.     
  86.     return (max(xResolutions), max(yResolutions))
  87.  
  88. def compareFrameBuffer(virtual):
  89.     x = virtual[0]
  90.     y = virtual[1]
  91.     p1 = Popen(['xrandr', '-q'], stdout=PIPE)
  92.     p = p1.communicate()[0]
  93.     a = p.split('\n')
  94.     b = a[0].split(',')
  95.     c = b[2].strip().split(' ')
  96.     c.remove('maximum')
  97.     c.remove('x')
  98.     xfb = int(c[0])
  99.     yfb = int(c[1])
  100.     if x > xfb or y > yfb:
  101.         '''
  102.         Call the GUI with policykit
  103.         '''
  104.         #Set the Virtual Resolution to str(x), str(y)
  105.         return [True, (str(x), str(y))]
  106.     else:
  107.         #Virtual Resolution is not required
  108.         return [False]
  109.  
  110.  
  111. def get_xkit_service(widget=None):
  112.     '''
  113.     returns a dbus interface to the screenresolution mechanism
  114.     '''
  115.     policy_auth = PolicyKitAuthentication()    
  116.  
  117.     granted = policy_auth.obtain_authorization(POLICY_KIT_ACTION, widget)
  118.     logging.debug("granted = %s" % granted)
  119.  
  120.     if not granted:
  121.         return None
  122.     
  123.     service_object = dbus.SystemBus().get_object(SERVICE_NAME, OBJECT_PATH)
  124.     service = dbus.Interface(service_object, INTERFACE_NAME)
  125.  
  126.     return service
  127.  
  128. def gui_dialog(message, parent_dialog,
  129.                       message_type=None,
  130.                       widget=None, page=0, 
  131.                       broken_widget=None):
  132.     '''
  133.     Displays an error dialog.
  134.     '''
  135.     if message_type == 'error':
  136.         message_type = gtk.MESSAGE_ERROR
  137.         logging.error(message)
  138.     elif message_type == 'info':
  139.         message_type = gtk.MESSAGE_INFO
  140.         logging.info(message)
  141.  
  142.  
  143.     
  144.         
  145.     dialog = gtk.MessageDialog(parent_dialog,
  146.                                gtk.DIALOG_MODAL|gtk.DIALOG_DESTROY_WITH_PARENT,
  147.                                message_type, gtk.BUTTONS_OK,
  148.                                message)
  149.     
  150.     translation = ui.AbstractUI()
  151.     
  152.     dialog.set_title(translation.string_title)
  153.     
  154.     if widget != None:
  155.         if isinstance (widget, gtk.CList):
  156.             widget.select_row (page, 0)
  157.         elif isinstance (widget, gtk.Notebook):
  158.             widget.set_current_page (page)
  159.     if broken_widget != None:
  160.         broken_widget.grab_focus ()
  161.         if isinstance (broken_widget, gtk.Entry):
  162.             broken_widget.select_region (0, -1)
  163.  
  164.     if parent_dialog:
  165.         dialog.set_position (gtk.WIN_POS_CENTER_ON_PARENT)
  166.         dialog.set_transient_for(parent_dialog)
  167.     else:
  168.         dialog.set_position (gtk.WIN_POS_CENTER)
  169.  
  170.     ret = dialog.run ()
  171.     dialog.destroy()    
  172.  
  173.     return ret
  174.  
  175.  
  176.  
  177. class BootWindow:
  178.     def __init__(self, resolution):
  179.         
  180.         translation = ui.AbstractUI()
  181.         self.permission_text = translation.string_permission_text
  182.         self.dbus_cant_connect = translation.string_dbus_cant_connect
  183.         self.operation_complete = translation.string_operation_complete
  184.         self.cant_apply_settings = translation.string_cant_apply_settings
  185.         
  186.         self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  187.         self.window.connect("delete_event", self.on_delete_event)
  188.         self.window.connect("destroy", self.on_destroy)
  189.         
  190.         self.window.set_border_width(20)
  191.         
  192.         self.window.set_title(translation.string_title)
  193.         self.window.set_position(gtk.WIN_POS_CENTER)
  194.         
  195.         gtk.window_set_default_icon_from_file("/usr/share/icons/hicolor/16x16/apps/gnome-display-properties.png")
  196.         
  197.         vbox = gtk.VBox(spacing=20)
  198.         self.resolution = resolution
  199.         self.label = gtk.Label(self.permission_text)
  200.         self.label.set_line_wrap(True)
  201.         self.label.set_justify(gtk.JUSTIFY_FILL)
  202.         self.button1 = gtk.Button(label=None, stock='gtk-yes', use_underline=False)
  203.         self.button1.connect("clicked", self.on_button1_clicked, None)
  204.         self.button1.show()
  205.         self.button2 = gtk.Button(label=None, stock='gtk-no', use_underline=False)
  206.         self.button2.connect("clicked", self.on_button2_clicked, None)
  207.         self.button2.show()
  208.         
  209.         buttonbox = gtk.HButtonBox()
  210.         buttonbox.set_layout(gtk.BUTTONBOX_END)
  211.         buttonbox.set_spacing(10)
  212.         buttonbox.pack_start(self.button2)
  213.         buttonbox.pack_start(self.button1)
  214.         buttonbox.show()
  215.         vbox.pack_start(self.label)
  216.         vbox.pack_start(buttonbox)
  217.         self.window.add(vbox)
  218.         self.label.show()
  219.         
  220.         vbox.show()
  221.     
  222.     def on_button1_clicked(self, widget, data=None):
  223.         self.window.hide()
  224.         self.conf = get_xkit_service()
  225.         if not self.conf:
  226. #            gui_dialog(self.dbus_cant_connect,
  227. #                              self.window, message_type='error')
  228.             sys.exit(1)
  229.         status = self.conf.setVirtual(self.resolution)
  230.         #print 'Status', status
  231.         if status == True:
  232.             #gui_dialog(self.operation_complete, self.window, message_type='info')
  233.             global clean
  234.             clean = True
  235.             gtk.main_quit()
  236.             #sys.exit(0)
  237.  
  238.         else:
  239.             #gui_dialog(self.cant_apply_settings, self.window, message_type='error')
  240.             #sys.exit(1)
  241.             gtk.main_quit()
  242.         
  243.     def on_button2_clicked(self, widget, data=None):
  244.         self.window.hide()
  245. #        gui_dialog(self.cant_apply_settings, self.window, message_type='error')
  246.         #sys.exit(1)
  247.         gtk.main_quit()
  248.         
  249.         
  250.     def on_delete_event(self, widget, event, data=None):
  251.         # Close the window:
  252.         return False
  253.  
  254.     def on_destroy(self, widget, data=None):
  255.         gtk.main_quit()
  256.  
  257.     def show(self):
  258.        self.window.show()
  259.  
  260.  
  261.  
  262.  
  263. if __name__ == "__main__":
  264.     if len(sys.argv) > 1:
  265.         for param in sys.argv[1:]:
  266.             if 'x' not in param:
  267.                 sys.exit(0)    
  268.     else:
  269.         sys.exit(0)
  270.     
  271.     res = sys.argv[1]
  272.     res = res.strip().split('x')
  273.     if checkVirtual(res):
  274.         sys.exit(0)
  275.     window = BootWindow(res)
  276.     window.show()
  277.     gtk.main()
  278.     if not clean:
  279.         sys.exit(1)
  280.     #call policy_kit
  281.